package com.remoting.core;

import com.remoting.api.RemoteServiceExecutor;
import com.remoting.api.RemoteSetting;
import com.remoting.api.expression.ExpressionParser;
import com.remoting.api.http.HttpHandler;
import com.remoting.api.registry.ServiceImportRegistry;
import com.remoting.api.registry.bean.ServiceImportInfo;
import com.remoting.api.request.RequestHandler;
import com.remoting.api.request.support.RemoteRequest;
import com.remoting.api.response.ResponseHandler;
import com.remoting.api.spi.expression.ExpressionParserFactory;
import com.remoting.api.spi.http.HttpHandlerFactory;
import com.remoting.api.spi.registry.ServiceImportRegistryBuilderFactory;
import com.remoting.api.spi.request.RequestHandlerFactory;
import com.remoting.api.spi.response.ResponseHandlerFactory;
import com.remoting.core.support.Assert;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;



/**
 * 远程服务调用器基础实现
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @since 2017年3月3日上午10:18:34
 */
public abstract class BaseRemoteServiceExecutor implements RemoteServiceExecutor {
	protected static final Log logger = LogFactory.getLog(BaseRemoteServiceExecutor.class);
	/** 远程服务注册信息 */
	private ServiceImportRegistry serviceImportRegistry;
	/** 表达式解析 */
	private ExpressionParser expressionParser;
	/** Http处理器 */
	private HttpHandler httpHandler;
	/** 请求处理器 */
	private RequestHandler requestHandler;
	/** 响应处理器 */
	private ResponseHandler reponseHandler;
	/** 配置参数*/
	private RemoteSetting remoteSetting;
	/**
	 * 构造器
	 */
	public BaseRemoteServiceExecutor(RemoteSetting remoteSetting) {
		this.serviceImportRegistry = ServiceImportRegistryBuilderFactory.create().build(remoteSetting.getConfigLocation());
		this.expressionParser = ExpressionParserFactory.create();
		this.httpHandler = HttpHandlerFactory.create().init(remoteSetting);
		this.requestHandler = RequestHandlerFactory.create().init(this.expressionParser);
		this.reponseHandler = ResponseHandlerFactory.create().init(this.expressionParser);
		this.remoteSetting = remoteSetting;
	}
	@Override
	public Object execute(String serviceId, Map<String,Object> source) {
		ServiceImportInfo serviceImportInfo = this.serviceImportRegistry.getServiceImportInfo(serviceId);
		Assert.notNull(serviceImportInfo,"未找到对应的服务信息，请检查服务导入配置文件。服务ID:" + serviceId);
		String result = doRequest(new RemoteRequest(this.requestHandler,serviceImportInfo, source));
		return this.reponseHandler.handle(result);
	}
   
	public ExpressionParser getExpressionParser() {
		return expressionParser;
	}
	public void setExpressionParser(ExpressionParser expressionParser) {
		this.expressionParser = expressionParser;
	}

	public HttpHandler getHttpHandler() {
		return httpHandler;
	}

	public void setHttpHandler(HttpHandler httpHandler) {
		this.httpHandler = httpHandler;
	}
	
    public RemoteSetting getRemoteSetting() {
		return remoteSetting;
	}
	/**
     * 执行请求
     * <p>说明:</p>
     * <li></li>
     * @author DuanYong
     * @param httpRequest
     * @return
     * @since 2017年10月18日上午10:55:33
     */
	protected abstract String doRequest(RemoteRequest httpRequest);
}
